<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="关联用户" required>
        <el-select
          v-model="form.username"
          :multiple="false"
          filterable
          remote
          reserve-keyword
          placeholder="请输入关键词"
          :remote-method="searchData"
          :loading="loading"
          @change="selectedData"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname" required>
        <el-input v-model="form.nickname" placeholder="请输入客服昵称"/>
      </el-form-item>
      <el-form-item label="头像" prop="avatar" required>
        <image-upload
          :file-size="1"
          :is-multiple="false"
          :limit="1"
          :value="form.avatar"
          :file-type="['png', 'jpg', 'jpeg','gif']"
          @input="afterUpload"
        ></image-upload>
        <el-input v-model="form.avatar" placeholder="头像url"/>
        <span class="el-form-item__info">图片尺寸：50*50</span>
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="form.mobile" placeholder="请输入手机号"/>
      </el-form-item>

      <el-form-item label="微信号">
        <el-input v-model="form.wechat" placeholder="请输入微信号"/>
      </el-form-item>
      <el-form-item label="QQ号">
        <el-input v-model="form.qq" placeholder="请输入QQ号"/>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email" placeholder="请输入邮箱"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" rows="3"/>
      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item style="margin-top: 30px">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button class="bs-left-10" @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/kefu'
import user_api from '@/api/member'
import ImageUpload from '@/components/UploadFile/image.vue'

export default {
  components: {
    ImageUpload
  },
  data() {
    return {
      options: [],
      list: [],
      loading: false,
      form: {
        user_id: 0,
        username: '',
        status: 1,
        kefu_id: 0,
        nickname: '',
        avatar: '',
        mobile: '',
        wechat: '',
        qq: '',
        email: '',
        remark: ''
      }
    }
  },

  mounted() {
    if (this.$route.query.hasOwnProperty('id')) {
      this.getData(this.$route.query.id)
    }
  },

  methods: {
    selectedData(value){
      this.form.user_id = value
    },
    searchData(query) {
      if (query !== '') {
        this.loading = true
        user_api.options({ from: 'kefu', keywords: query })
          .then((res) => {
              if (res.code === 0) {
                this.options = res.data.list
              } else {
                this.options = []
              }
            }
          ).finally(() => {
            this.loading = false
        })
      } else {
        this.options = []
      }
    },
    afterUpload(list) {
      if (!list) return
      const file = list[0]
      console.log(file)
      if (file) {
        this.form.avatar = file.url
      }
    },
    getData(id) {
      api.info({ id: id }).then((res) => {
        this.form = res.data.info
      })
    },

    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let req
          if (this.form.id > 0) {
            req = api.update(this.form)
          } else {
            req = api.create(this.form)
          }
          req.then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              duration: 500,
              onClose: () => {
                this.$router.go(-1)
              }
            })
          }).catch((error) => {

          })
        }
      })

    }
  }
}
</script>

<style scoped>

.article-textarea ::v-deep {

textarea {
  padding-right: 40px;
  resize: none;
  border: none;
  border-radius: 0px;
  border-bottom: 1px solid #bfcbd9;
}

}
</style>

